package com.easybuilder.common.utils;

import com.easybuilder.core.table.entity.SysTable;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.concurrent.TimeUnit;

@Component
public class RedisUtil {

    @Autowired
    private RedisTemplate redisTemplate;

    private static RedisTemplate staticRedisTemplate;

    @PostConstruct
    private void init() {
        staticRedisTemplate = redisTemplate;
    }

    /** 设置key value*/
    public static void setValue(Object key, Object value){
        staticRedisTemplate.opsForValue().set(key, value);
    }

    /** 设置key value 带超时时间*/
    public static void setValue(Object key, Object value, Long time, TimeUnit timeUnit){
        staticRedisTemplate.opsForValue().set(key, value, time, timeUnit);
    }

    /** 获取值 泛型转换 **/
    public static <T> T getValue(Object key, Class<T> klass) {
        Object obj = staticRedisTemplate.opsForValue().get(key);
        if (obj != null) {
            return (T) obj;
        }
        return null;
    }

}
